import { login } from "@/api/login";
// import { submitRes } from '@/api/index'
const user = {
  state: {
    token: localStorage.getItem("client_authorization") || "",
    is_login: +sessionStorage.getItem("is_login") || 1,
    is_dd: +sessionStorage.getItem("is_dd") || 1
  },
  mutations: {
    SET_DATA: (state, { key, data }) => {
      state[key] = data;
    }
  },

  actions: {
    // 登录
    login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        if (process.env.VUE_APP_PREVIEW == "true") {
          login({ authCode: "" })
            .then(res => {
              if (res.code == 200) {
                localStorage.setItem("current_user", JSON.stringify(res.data));
                localStorage.setItem("client_authorization", res.data.access_token);
                commit("SET_DATA", { key: "token", data: res.data.access_token });
                resolve(res);
              } else {
                reject(res);
              }
            })
            .catch(error => {
              reject(error);
            });
        } else {
          dd.getAuthCode({
            corpId: ""
          })
            .then(data => {
              login({ authCode: data.code })
                .then(res => {
                  if (res.code == 200) {
                    localStorage.setItem("current_user", JSON.stringify(res.data));
                    localStorage.setItem("client_authorization", res.data.access_token);
                    commit("SET_DATA", { key: "token", data: res.data.access_token });
                    resolve(res);
                  } else {
                    reject(res);
                  }
                })
                .catch(error => {
                  reject(error);
                });
            })
            .catch(err => {
              alert(JSON.stringify(err));
            });
        }
      });
    }
    // 登出
    // Logout({ commit }) {
    //   return new Promise(resolve => {
    //     logout()
    //       .then(() => {
    //         commit("SET_TOKEN", "");
    //         localStorage.removeItem("client_authorization");
    //         localStorage.removeItem("userName");
    //         localStorage.removeItem("sceneList");
    //         localStorage.removeItem("areaLevel");
    //         localStorage.removeItem("roleName");
    //         localStorage.removeItem("roleDate");
    //         resolve();
    //       })
    //       .catch(() => {
    //         resolve();
    //       })
    //       .finally(() => {});
    //   });
    // }
  }
};

export default user;

